package risk.client.logic;

import java.awt.Color;

import java.util.Iterator;
import java.util.List;

import risk.client.Risk;

public class ComputerAttacker extends Computer {
    public ComputerAttacker(int i, String string, Color color, Risk risk) {
        super(i, string, color, risk);
    }

    @Override
    protected Node targetNode() {
        //List with nodes connected to the first choosen one
        List<Node> list = rememberNode.getConnections();
        Iterator i = list.iterator();
        Node node = null, nodeOwn = null, nodeOpp = null; 
        
        // Loop through connected nodes (get first connected node)
        while(i.hasNext()) {
            node = (Node)i.next();
            
            /*
             * If connected node is owned by computer we want to
             * check if that node has less units than the previous selected one
             * (or if we havn't selected a previous one)
             */
            if(node.getArmy().getOwner().getId() == getId()) {
                if(nodeOwn == null || nodeOwn.getArmy().getUnits() < node.getArmy().getUnits()) {
                    nodeOwn = node;
                }
            } else {
                // Same if the connected node is an opponent node
                if(nodeOpp == null) {
                    nodeOpp = node;
                }
            }
        } // End while i.hasNext()
        
        // nodeOpp is the first opponent node, nodeOwn is the first of our own nodes
        // Attack has priority
        return (nodeOpp != null) ? nodeOpp : nodeOwn;
    }
}
